<?php

/**
 * 
 *
 * @version $Id: class_bbcode.inc.php,v 1.3 2004/08/21 13:43:25 phppc Exp $
 * @copyright 2004 
 **/

class bb_code extends phpmc{
	var $suchen = array();
	var $ersetzen = array();
	/**
	 * bb_code::bbcode_create_tag()
	 * 
	 * @param $tag
	 * @param $repl
	 * @param string $type
	 * @param integer $anzahl_parameter
	 * @param string $restrict_m
	 * @param string $restrict_p
	 * @param string $special_search
	 * @param string $int
	 * @return 
	 **/
	function bbcode_create_tag($tag,$repl,$type = "",$anzahl_parameter = 0,$restrict_m = ".*",$restrict_p = ".*",$special_search = "",$int = "i") {
		if ($type == "") {
		    if ($special_search == "") {
				if ($anzahl_parameter == 0) {
					$this->suchen[] = "!\[$tag\]($restrict_m)\[\/$tag\]!$int";
				}elseif ($anzahl_parameter == 1) {
					$this->suchen[] = "!\[$tag\=($restrict_p)\]($restrict_m)\[\/$tag\]!$int";
				}    
			}else $this->suchen[] = $special_search;
			$this->ersetzen[] = $repl;
		}elseif($type == "list") {
			$this->suchen[] = "!\[list\=(1|a)\](.*)\[\/list\]!esi";
			$this->ersetzen[] = "\$this->list_code(stripslashes('\\1'),stripslashes('\\2'))";
		}
		
	}
	/**
	 * bb_code::bbcode_do_code()
	 * 
	 * @param $text
	 * @return 
	 **/
	function bbcode_do_code ($text) { // normal bbcode
		$text = str_replace("&amp;","&",$text);
    	/**
 * $suchen = array("!\[(b|i|u|center)\](.*)\[\/(b|i|u|center)\]!siU",
 *                                                 "!\[URL\]((?:http\:\/\/){1}(((www)?)|(([a-z])*))(\.){0,1}([a-z])+(\.){1}([a-z]){2,4}([a-z-_0-9./?=&]*))\[/URL\]!i",
 * 												"!\[URL\=((?:http\:\/\/){1}(?:(?:(?:www)?)|(?:(?:[a-z])*))(?:\.){0,1}(?:[a-z])+(?:\.){1}(?:[a-z]){2,4}([a-z-_0-9./?=&]*))\]([a-z_0-9.]+)\[\/URL\]!i",
 *                                                 "!\[php\](.*)\[/php\]!esiU",
 * 												"!\[list\=(1|a)\](.*)\[\/list\]!esi");
 *         $ersetzen = array("<".strtolower("\\1").">\\2</".strtolower("\\1").">","<a href=\"\\1\" target=\"_blank\">\\1</a>","<a href=\"\\1\" target=\"_blank\">\\3</a>",
 *                                                         "\$this->php_code(stripslashes('\\1'))","\$this->list_code(stripslashes('\\1'),stripslashes('\\2'))");
 */
 		return preg_replace($this->suchen,$this->ersetzen,$text);
    }
    /**
     * bb_code::php_code()
     * 
     * @param $php_code
     * @return 
     **/
    function php_code ($php_code) {
        $php_code = stripslashes($php_code);
        $php_code = "<?php\n$php_code\n?>";
	    ob_start();
        highlight_string($php_code);
        $php_code = ob_get_contents();
        ob_end_clean();
        $teile = explode("<br />",$php_code);
        $i = 1;
        unset ($php_code);
        foreach ($teile as $parts) {
    	    $php_code .= "<tr><td class=\"php\" align=\"right\">$i</td><td class=\"php\">$parts</td></tr>";
            $i++;
        }
        $php_code = str_replace("<br />","",$php_code);
        $php_code = stripslashes($php_code);
        $php_code = str_replace("&quot;","\"",$php_code);
        $php_code = str_replace("&gt;",">",$php_code);
        $php_code = str_replace("&amp;","&",$php_code);
        return "<table class=\"php\"><tr><td class=\"php\" colspan=\"2\">PHP-Code</td></tr>" . $php_code . "</table>";
    }
	/**
	 * bb_code::list_code()
	 * 
	 * @param $list_type
	 * @param $list
	 * @return 
	 **/
	function list_code ($list_type,$list) {
		$list_parts = preg_replace("!\[\*\]([a-z_0-9\-]+)!i","<li>\\1</li>",$list);
		if ($list_type == "a") $html_tag = "ul";
		elseif($list_type == "1") $html_tag = "ol";
		return "<$html_tag>" . $list_parts . "</$html_tag>";
	}
}

?>